C++ Core Guidelines Explained: Best Practices for Modern C++ by Rainer Grimm

C++ Core Guidelines Explained: Best Practices for Modern C++ by Rainer Grimm

Author:Rainer Grimm [Rainer Grimm]
Language: eng
Format: epub, mobi, pdf
Publisher: Addison-Wesley Professional
Published: 2022-03-24T16:00:00+00:00


Figure 8.13. Modulo versus overflow with unsigned’s and signed’s

Detecting overflow

You may have a burning question: How can you detect an overflow? Quite easy! Replace the erroneous assignment x += 1000 with an expression using curly braces: x = {x + 1000}.

The difference is that the compiler checks for narrowing conversions and, therefore, detects the overflow. Here is the output from GCC.

Figure 8.14. Detecting narrowing conversion

Typical arithmetic errors

The following three rules always result in undefined behavior.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.